package com.qf.zyh.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.zyh.common.ResponseResult;
import com.qf.zyh.common.ResultType;
import com.qf.zyh.dao.CustomerMapper;
import com.qf.zyh.entity.Customer;
import com.qf.zyh.services.CustomerServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServicesImpl implements CustomerServices {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ResultType tableCus(Integer page, Integer limit, String username, Integer state) {
        PageHelper.startPage(page,limit);
        List<Customer> customerList = customerMapper.selectByNameByState(username,state);
        PageInfo pageInfo = new PageInfo(customerList);
        return new ResultType(0,"",(int)pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public ResponseResult delCus(Integer[] ids) {
        customerMapper.deleteByIds(ids);
        return ResponseResult.getSuccess();
    }

    @Override
    public ResponseResult addCus(Customer c) {
        int i = customerMapper.insertSelective(c);
        if (i>0) {
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("出错惹");
    }

    @Override
    public ResponseResult updateCus(Customer c) {
        int i = customerMapper.updateByPrimaryKeySelective(c);
        if (i>0) {
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("出错惹");
    }

    @Override
    public List<Customer> tree() {
        List<Customer> list = customerMapper.gettree();
        if (list!=null) {
            return list;
        }
        return null;
    }
}
